package com.wk.pms.controller;

import com.wk.common.bean.PageVo;
import com.wk.common.bean.QueryCondition;
import com.wk.common.bean.Resp;
import com.wk.pms.entity.Attr;
import com.wk.pms.service.AttrService;
import com.wk.pms.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    /**
     * 根据属性类型(默认基本类型)和分类ID查询商品属性
     */
    @GetMapping
    public Resp<PageVo> queryByCategoryId(@RequestParam(value = "type",defaultValue = "1") Integer type,
                                          @RequestParam("cid") Long categoryId,
                                          QueryCondition queryCondition){
        PageVo page = attrService.queryByCategoryId(queryCondition,type,categoryId);

        return Resp.ok(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public Resp<Attr> info(@PathVariable("attrId") Long attrId){
        Attr attr = attrService.getById(attrId);

        return Resp.ok(attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Resp<Object> save(@RequestBody AttrVO attrVo){
        attrService.saveAttr(attrVo);
        return Resp.ok(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Resp<Object> update(@RequestBody Attr attr){
        attrService.updateById(attr);

        return Resp.ok(null);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Resp<Object> delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return Resp.ok(null);
    }

}

